﻿using HPIT.RentHouse.Admin.Filters;
using HPIT.RentHouse.Admin.Models;
using HPIT.RentHouse.DTO;
using HPIT.RentHouse.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace HPIT.RentHouse.Admin.Controllers
{
    [Authorize]
    public class RolesController : Controller
    {
        private readonly IPermissionsService _permissionsService;
        private readonly IRolesService _rolesService;
        public RolesController(IPermissionsService permissionsService, IRolesService rolesService)
        {
            _permissionsService = permissionsService;
            _rolesService = rolesService;
        }

        //[CheckMyPermission("lookRole")]
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetList(int start, int length, string name)
        {
            int rowCount = 0;
            var list = _rolesService.GetPageList(start, length, name, ref rowCount);
            PageModel pageModel = new PageModel();
            pageModel.data = list;
            pageModel.recordsTotal = rowCount;
            pageModel.recordsFiltered = rowCount;
            return Json(pageModel);
        }

        public ActionResult Add()
        {
            List<PermissionDTO> perList = _permissionsService.GetList();
            ViewBag.perList = perList;
            return View();
        }

        [HttpPost]
        public ActionResult AddRole(RoleEditDTO dto)
        {
            var result = _rolesService.Add(dto);
            return Json(result);
        }

        public ActionResult Edit(long id)
        {
            RoleEditDTO model = _rolesService.Get(id);
            var perList = _permissionsService.GetList();
            ViewBag.perList = perList;
            return View(model);
        }

        [HttpPost]
        public ActionResult UpdateRole(RoleEditDTO dto)
        {
            var result = _rolesService.Update(dto);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(long id)
        {
            var result = _rolesService.Delete(id);
            return Json(result);
        }

        [HttpPost]
        public ActionResult DeleteBatch(List<long> ids)
        {
            var result = _rolesService.DeleteBatch(ids);
            return Json(result);
        }
    }
}